package com.dhl.fin.api.controller;

import com.dhl.fin.api.common.controller.CommonController;
import com.dhl.fin.api.common.domain.BasicDomain;
import com.dhl.fin.api.common.dto.ApiResponse;
import com.dhl.fin.api.common.dto.QueryDto;
import com.dhl.fin.api.common.util.MapUtil;
import com.dhl.fin.api.common.util.ObjectUtil;
import com.dhl.fin.api.common.util.WebUtil;
import com.dhl.fin.api.domain.Consult;
import com.dhl.fin.api.domain.ConsultChat;
import com.dhl.fin.api.domain.Family;
import com.dhl.fin.api.enums.ConsultChatStatusEnum;
import com.dhl.fin.api.service.ConsultChatService;
import com.dhl.fin.api.service.ConsultService;
import com.dhl.fin.api.service.FamilyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.support.Querydsl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @author CuiJianbo
 * @since 2025/3/16
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("consult")
public class ConsultController extends CommonController<Consult> {

    @Autowired
    private ConsultChatService consultChatService;

    @Autowired
    private FamilyService familyService;

    @Autowired
    private ConsultService consultService;


    @ResponseBody
    @RequestMapping("chatData")
    public ApiResponse getConsultChatData(Long consultId) throws Exception {

        if (ObjectUtil.isNull(consultId)) {
            return ApiResponse.success();
        }

        List<ConsultChat> consultChatList = consultChatService.getChatData(consultId);
        ConsultChat consultChat = consultChatList.stream()
                .filter(p -> p.getOwner().equals("system"))
                .sorted(Comparator.comparing(BasicDomain::getCreateTime).reversed())
                .findFirst()
                .orElse(null);
        if (ObjectUtil.notNull(consultChat)) {
            consultChat.setShowReply(true);
        }

        Consult consult = consultService.get(consultId);

        Family family = familyService.get(QueryDto.builder()
                .available()
                .addWhere("wx_account_code = '" + WebUtil.getLoginUser().getUuid() + "'")
                .build());

        Map result = MapUtil.builder()
                .add("title", consult.getTitle())
                .add("chatData", consultChatList)
                .add("headPicPath", family.getHeadPicPath())
                .build();


        return ApiResponse.success(result);
    }

    @ResponseBody
    @RequestMapping("viewed")
    public ApiResponse setConsultToViewed(Long consultId) throws Exception {

        Consult consult = consultService.get(consultId);

        //更新为已查看
        if (consult.getReplyStatus().equals(ConsultChatStatusEnum.REPLIED.getCode())) {
            consultService.setConsultViewed(consultId);
        }

        return ApiResponse.success();

    }
}